<template>
  <div style=" width: 110%; margin: auto;">
    <h3>资产负债表</h3>
    <h4>（截止到{{year}}年 12 月 31 日） 单位：人民币 元 </h4>
<!--    <span style="color:red;">*没有则填0</span>-->
    <el-row style="margin-top: 10px;">
      <el-col :span="12" style="margin: 0px;">
        <el-table :header-cell-style="{background: '#F0F5FA',color: '#333333'}" size="default" style="margin: 0px;" :data="leftList" border>
<!--          <el-table-column type="index" label="序号" align="center" width="60" />-->
          <el-table-column prop="assetTermName" label="资产" align="center">
          </el-table-column>
          <el-table-column prop="fieldSort" label="行次" align="center" width="50" />
          <el-table-column prop="startBalance" label="年初数" align="center">
            <template #default="scope">
              <el-input @input="leftStartBalance()"
                        v-show="scope.row.fieldSort != 1 && scope.row.fieldSort != 11 && scope.row.fieldSort != 15 && scope.row.fieldSort != 23 && scope.row.fieldSort != 25 "
                        :disabled="isInputByTitle(scope.row.assetTermName)"  :precision="2" size="small" style="width: 98%;" v-model="scope.row.startBalance"
                        placeholder=""></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="endBalance" label="年末数" align="center">
            <template #default="scope">
              <el-input @input="leftEndBalance()"
                        v-show="scope.row.fieldSort != 1 && scope.row.fieldSort != 11 && scope.row.fieldSort != 15 && scope.row.fieldSort != 23 && scope.row.fieldSort != 25"
                        :disabled="isInputByTitle(scope.row.assetTermName)"  :precision="2" size="small" style="width: 98%;" v-model="scope.row.endBalance"
                        placeholder=""></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="12" style="margin: 0px;">
        <el-table  :header-cell-style="{background: '#F0F5FA',color: '#333333'}"
          style="margin: 0px;"  size="default" :data="rightList" border>
          <el-table-column prop="assetTermName" label="负债和净资产" align="center" />
          <el-table-column prop="fieldSort" label="行次" align="center" width="50" />
          <el-table-column prop="startBalance" label="年初数" align="center">
            <template #default="scope">
              <el-input
                @input="rightStartBalance()"
                v-show="scope.row.fieldSort != 1 && scope.row.fieldSort != 13 && scope.row.fieldSort != 18 && scope.row.fieldSort != 21"
                :disabled="isInputByTitle(scope.row.assetTermName)"  :precision="2" size="small" style="width: 98%;" v-model="scope.row.startBalance"
                placeholder=""></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="endBalance" label="年末数" align="center">
            <template #default="scope">
              <el-input
                @input="rightEndBalance()"
                v-show="scope.row.fieldSort != 1 && scope.row.fieldSort != 13 && scope.row.fieldSort != 18 && scope.row.fieldSort != 21"
                :disabled="isInputByTitle(scope.row.assetTermName)"  :precision="2" size="small" style="width: 98%;" v-model="scope.row.endBalance"
                placeholder=""></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>
<script>

import { getTableInfoByPage, mfSaveAssets } from '@/views/yearCheck/ykb/mf/mf'

export default {
  data() {
    return {
      year:localStorage.getItem('examineYear'),
      listForm: undefined,
      leftList: [],
      rightList: []
    }
  },

  created() {

  },
  methods: {
    rightStartBalance() {
      let data1 = [this.getStr(this.rightList[1].startBalance), this.getStr(this.rightList[2].startBalance),
        this.getStr(this.rightList[3].startBalance), this.getStr(this.rightList[4].startBalance),
        this.getStr(this.rightList[5].startBalance), this.getStr(this.rightList[6].startBalance),
        this.getStr(this.rightList[7].startBalance), this.getStr(this.rightList[8].startBalance),
        this.getStr(this.rightList[9].startBalance), this.getStr(this.rightList[10].startBalance)]
      this.rightList[11].startBalance = 0
      for (let i = 0; i < data1.length; i++) {
        this.rightList[11].startBalance += data1[i]
      }
      this.rightList[16].startBalance = this.getStr(this.rightList[13].startBalance) + this.getStr(this.rightList[14].startBalance) + this.getStr(this.rightList[15].startBalance);
      this.rightList[19].startBalance = this.getStr(this.rightList[11].startBalance) + this.getStr(this.rightList[16].startBalance) + this.getStr(this.rightList[18].startBalance);
      this.rightList[23].startBalance = this.getStr(this.rightList[21].startBalance) + this.getStr(this.rightList[22].startBalance);
      this.rightList[24].startBalance = this.getStr(this.rightList[19].startBalance) + this.getStr(this.rightList[23].startBalance);
    },
    rightEndBalance() {
      let data1 = [this.getStr(this.rightList[1].endBalance), this.getStr(this.rightList[2].endBalance),
        this.getStr(this.rightList[3].endBalance), this.getStr(this.rightList[4].endBalance),
        this.getStr(this.rightList[5].endBalance), this.getStr(this.rightList[6].endBalance),
        this.getStr(this.rightList[7].endBalance), this.getStr(this.rightList[8].endBalance),
        this.getStr(this.rightList[9].endBalance), this.getStr(this.rightList[10].endBalance)]
      this.rightList[11].endBalance = 0
      for (let i = 0; i < data1.length; i++) {
        this.rightList[11].endBalance += data1[i]
      }
      this.rightList[16].endBalance = this.getStr(this.rightList[13].endBalance) + this.getStr(this.rightList[14].endBalance) + this.getStr(this.rightList[15].endBalance)
      this.rightList[19].endBalance = this.getStr(this.rightList[11].endBalance) + this.getStr(this.rightList[16].endBalance) + this.getStr(this.rightList[18].endBalance)
      this.rightList[23].endBalance = this.getStr(this.rightList[21].endBalance) + this.getStr(this.rightList[22].endBalance)
      this.rightList[24].endBalance = this.getStr(this.rightList[19].endBalance) + this.getStr(this.rightList[23].endBalance)
    },
    leftStartBalance() {
      let data1 = [this.getStr(this.leftList[1].startBalance), this.getStr(this.leftList[2].startBalance),
        this.getStr(this.leftList[3].startBalance), this.getStr(this.leftList[4].startBalance),
        this.getStr(this.leftList[5].startBalance), this.getStr(this.leftList[6].startBalance),
        this.getStr(this.leftList[7].startBalance), this.getStr(this.leftList[8].startBalance)]
      this.leftList[9].startBalance = 0
      console.log(data1)
      for (let i = 0; i < data1.length; i++) {
        this.leftList[9].startBalance += data1[i]
      }
      this.leftList[13].startBalance = this.getStr(this.leftList[11].startBalance) + this.getStr(this.leftList[12].startBalance)
      this.leftList[17].startBalance = this.getStr(this.leftList[15].startBalance) - this.getStr(this.leftList[16].startBalance)
      this.leftList[21].startBalance = this.getStr(this.leftList[17].startBalance) + this.getStr(this.leftList[18].startBalance) + this.getStr(this.leftList[19].startBalance) + this.getStr(this.leftList[20].startBalance)
      this.leftList[26].startBalance = this.getStr(this.leftList[9].startBalance) + this.getStr(this.leftList[21].startBalance) +this.getStr(this.leftList[23].startBalance) +this.getStr(this.leftList[25].startBalance);
    },
    leftEndBalance() {
      let data1 = [this.getStr(this.leftList[1].endBalance), this.getStr(this.leftList[2].endBalance),
        this.getStr(this.leftList[3].endBalance), this.getStr(this.leftList[4].endBalance),
        this.getStr(this.leftList[5].endBalance), this.getStr(this.leftList[6].endBalance),
        this.getStr(this.leftList[7].endBalance), this.getStr(this.leftList[8].endBalance)]
      this.leftList[9].endBalance = 0
      for (let i = 0; i < data1.length; i++) {
        this.leftList[9].endBalance += data1[i]
      }
      this.leftList[13].endBalance = this.getStr(this.leftList[11].endBalance) + this.getStr(this.leftList[12].endBalance)
      this.leftList[17].endBalance = this.getStr(this.leftList[15].endBalance) - this.getStr(this.leftList[16].endBalance)
      this.leftList[21].endBalance = this.getStr(this.leftList[17].endBalance) + this.getStr(this.leftList[18].endBalance)
        + this.getStr(this.leftList[19].endBalance) + this.getStr(this.leftList[20].endBalance);
      this.leftList[26].endBalance = this.getStr(this.leftList[9].endBalance) + this.getStr(this.leftList[21].endBalance) + this.getStr(this.leftList[23].endBalance) +this.getStr(this.leftList[25].endBalance);
    },
    getStr(str) {
      if (str == null || str == '') {
        str = 0
      } else {
        str = parseFloat(str)
      }
      return str
    },
    postInfo() {
      for (let i = 0; i < this.listForm.length; i++) {
        if(this.listForm[i].startBalance == null || this.listForm[i].startBalance == ''){
          this.listForm[i].startBalance = 0;
        }
        if(this.listForm[i].endBalance == null || this.listForm[i].endBalance == ''){
          this.listForm[i].endBalance = 0;
        }
      }
      this.leftStartBalance();
      this.leftEndBalance();
      this.rightStartBalance();
      this.rightEndBalance();
      for (let i = 0; i < this.rightList.length; i++) {
        this.rightList[i].startBalance += '';
        this.rightList[i].endBalance += '';
      }
      if(this.rightList[21].startBalance == null || this.rightList[21].endBalance == ''){
        this.$message({
          message: '请填写非限定性资产！',
          type: 'error'
        })
        return
      }
      if(this.rightList[22].startBalance == null || this.rightList[22].endBalance == ''){
        this.$message({
          message: '请填写限定性净资产！',
          type: 'error'
        })
        return
      }
      if(this.rightList[23].startBalance == null || this.rightList[23].endBalance == ''){
        this.$message({
          message: '请填写净资产合计！',
          type: 'error'
        })
        return
      }
      if(this.rightList[24].startBalance == null || this.rightList[24].endBalance == ''){
        this.$message({
          message: '请填写负债和净资产合计！',
          type: 'error'
        })
        return
      }
      for (let i = 0; i < this.listForm.length; i++) {
        this.listForm[i].creditCode = localStorage.getItem('creditCode')
        this.listForm[i].examineYear = localStorage.getItem('examineYear')
        if(this.listForm[i].startBalance != null && this.listForm[i].startBalance != ''){
          this.listForm[i].startBalance = parseFloat(this.listForm[i].startBalance).toFixed(2);
        }
        if(this.listForm[i].endBalance != null && this.listForm[i].endBalance != ''){
          this.listForm[i].endBalance = parseFloat(this.listForm[i].endBalance).toFixed(2);
        }
      }
      mfSaveAssets(this.listForm).then(response => {
        this.$message({
          message: '保存资产负债表成功！',
          type: 'success'
        })
        this.getInfo();
      })
    },
    getInfo() {
      getTableInfoByPage(18).then(response => {
        this.listForm = JSON.parse(JSON.stringify(response.data).replace(/null/g, '""'));
        this.leftList = []
        this.rightList = []
        for (let i = 0; i < this.listForm.length; i++) {
          if (this.listForm[i].fieldType == 1) {
            this.leftList.push(this.listForm[i])
          } else {
            this.rightList.push(this.listForm[i])
          }
        }
        console.log(this.leftList)
        console.log(this.rightList)
      })
    },
    setTitle(value) {
      if (value.length > 7) {
        return value.substring(0, 6)
      }
      return value
    },
    isInputByTitle(title) {
      if (title == '流动资产合计') {
        return true
      } else if (title == '长期投资合计') {
        return true
      } else if (title == '固定资产净值') {
        return true
      } else if (title == '固定资产合计') {
        return true
      } else if (title == '资产总计') {
        return true
      } else if (title == '流动负债合计') {
        return true
      } else if (title == '长期负债合计') {
        return true
      } else if (title == '负债合计') {
        return true
      } else if (title == '净资产合计') {
        return true
      } else if (title == '负债和净资产总计') {
        return true
      }
      return false
    }
  }
}
</script>
<style scoped lang="scss">
.el-table {
  --el-table-row-hover-bg-color: rgba(39, 137, 238, 0.1);
  --el-table-border-color:#e0e5f0;
}
:deep(.el-card.is-always-shadow) {
  box-shadow: 0 1px 14px 0 rgba(209, 224, 239, 0.6)
}

/* 拉框选中 */
.el-select-dropdown__item {
  height: 24px;
  line-height: 24px;
  font-size: 12px;
}

/* 鼠标经过下拉框 */
.el-select-dropdown__item:hover {
  height: 24px;
  line-height: 24px;
  font-size: 12px;
}

.el-select-dropdown__item.selected {
  color: #2789EE;
}
.app-container {
  margin-top: 40px;
  margin-left: 15%;
  width: 70%;
}

.div-bg {
  width: 100%;
  background: #F0F0F0;
  height: 10px;
}

.box-card {
  width: 100%;
  margin-top: 20px;
  padding: 20px;
}

.el-col {
  padding-left: 5px;
}

.divider-title {
  font-weight: bold;
  font-size: 18px;
}

.custom-select .el-select-dropdown {
  max-height: 600px; /* 设置为你想要的高度 */
  overflow: auto; /* 添加滚动条 */
}

table {
  border-collapse: collapse; /* 确保表格的边框合并为单一边框 */
  width: 100%; /* 表格宽度 */
}

td {
  border: 1px solid #ababab; /* 设置单线边框 */
  padding: 6px; /* 单元格内边距 */
}

.ellipsis {
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.el-popover {
  height: 150px;
  overflow: auto;
}
.el-form-item {
  margin-bottom: 0px;
}
</style>
